//
//  CNBBSReplyListViewModel.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/7/19.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSReplyListViewModel.h"
#import <CNBaseTool/CNCategoryHeader.h>

@interface CNBBSReplyListViewModel ()

@property (nonatomic,strong) CNBBSReplyListModel *model;

@end

@implementation CNBBSReplyListViewModel
/**
 *  获取有多少个
 *
 *  @return 返回个数
 */
-(NSInteger)rowNumber
{
    return self.dataArr.count;
}

/**
 *  判断是否是最大页数
 *
 *
 */
- (BOOL)isHasMore{
    return self.model.data.count > 0;
}

#pragma mark - 数据请求

-(void)refreshBBSReplyListDataWithTocipId:(NSString *)tocipId IsInverted:(BOOL)isInverted CompletionHandle:(CompletionHandle)completionHandle
{
    self.page = 1;
    return [self getBBSReplyListDataFromNetWithTocipId:tocipId  IsInverted:isInverted CompleteHandle:completionHandle];
}

- (void)getMoreBBSReplyListDataWithTocipId:(NSString *)tocipId IsInverted:(BOOL)isInverted CompletionHandle:(CompletionHandle)completionHandle
{
    //如果当前页数已经是最大页数，那么没有必要再发送获取更多请求了，这样会浪费用户流量
//    if (self.isHasMore) {
        self.page += 1;
        [self getBBSReplyListDataFromNetWithTocipId:tocipId IsInverted:isInverted CompleteHandle:completionHandle];
//    }else{
//        NSError *err=[NSError errorWithDomain:@"" code:999 userInfo:@{NSLocalizedDescriptionKey:@"没有更多数据了"}];
//        completionHandle(err);
//    }
    
}

- (void)getBBSReplyListDataFromNetWithTocipId:(NSString *)tocipId IsInverted:(BOOL)isInverted CompleteHandle:(CompletionHandle)completionHandle
{
    self.dataTask = [CNBBSNetManager getBBSReplyListWithTocipId:tocipId AndPage:self.page IsInverted:isInverted completionHandle:^(CNBBSReplyListModel *model, NSError *error) {
        
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            
            if (self.code == 200) {
                self.model = model;
                if (self.page == 1) {
                    [self.dataArr removeAllObjects];
                }
                [self.dataArr addObjectsFromArray:model.data];
            }
            else
            {
                //为了防止token失效时页数加1导致重新添加数据时中间少页
                if (self.page > 1)
                {
                    self.page -= 1;
                }
            }
        }
        completionHandle(error);
    }];
}

/**
 *  获取CNSearchTagesDataDataModel
 *
 *  @param row 根据行
 *
 *  @return 返回CNSearchTagesDataDataModel
 */

-(CNBBSReplyListDataModel *)replyListDataModelForRowInReplyList:(NSInteger)row{
    if (row == 0) {
        return self.dataArr.firstObject;
    }
    return self.dataArr[row];
}

/** 正文 */
-(NSString *)bodyForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].body;
}


/** 赞数 */
-(NSString *)diggForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].digg;
}

/** 赞数 */
-(void)setDigg:(NSString*)digg ForRow:(NSInteger)row
{
    return [[self replyListDataModelForRowInReplyList:row] setDigg:digg];
}

/** 层数 */
-(NSString *)floorForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].floor;
}

/** 时间 */
-(NSString *)createdAtForRow:(NSInteger)row
{
    return [CSDNTool compareCurrentTime:[self replyListDataModelForRowInReplyList:row].created_at];
}

/** 昵称 */
-(NSString *)nickNameForRow:(NSInteger)row
{
    if (![self replyListDataModelForRowInReplyList:row].nickname || [[self replyListDataModelForRowInReplyList:row].nickname isEqualToString:@""])
    {
        return [self replyListDataModelForRowInReplyList:row].username;
    }
    return [self replyListDataModelForRowInReplyList:row].nickname;
}

/**  */
-(NSString *)pointForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].point;
}

/**  */
-(NSString *)postIdRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].post_id;
}

/** 用户名 */
-(NSString *)userNameForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].username;
}

/** 头像 */
-(NSString *)avatarForRow:(NSInteger)row
{
    return [[self replyListDataModelForRowInReplyList:row].avatar stringByReplacingOccurrencesOfString:@"http:" withString:@"https:"];
}

/**  */
-(NSString *)userIdForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].user_id;
}

/**是否赞  */
-(BOOL)isDiggedForRow:(NSInteger)row
{
    return [self replyListDataModelForRowInReplyList:row].is_digged;
}

/**是否赞  */
-(void)setIsDigged:(BOOL)isDigged ForRow:(NSInteger)row
{
    return [[self replyListDataModelForRowInReplyList:row] setIs_digged:isDigged];
}

@end
